﻿@{
    ViewData["Title"] = "商品分类增加或修改";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@model Category
@section css{
<style>
    .layui-form-item .layui-input {
        width: 350px;
        padding-right: 10px;
        line-height: 38px;
    }
    .layui-form-item .layui-textarea {
        width: 350px;
    }
    .layui-form-select {
        width: 350px;
    }
    .layui-upload-img {
        width: 135px;
        height: 95px;
        padding-left: 150px;
    }
</style>
}
<form class="layui-form" action="" lay-filter="column-edit">
    <div class="layuimini-container layuimini-page-anim">
        <div class="layuimini-main">
            <div class="layui-form layuimini-form">
                @if (Model.Id > 0)
                {
                    <div class="layui-form-item">
                        <label class="layui-form-label">编号</label>
                        <div class="layui-input-block">
                            <input type="text" name="id" lay-verify="id" value="@Model.Id" disabled class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label required">添加时间</label>
                        <div class="layui-input-block">
                            <input type="text" name="createTime" value="@Model.CreateTime.ToWebString()" readonly class="layui-input">
                        </div>
                    </div>
                }
                <div class="layui-form-item">
                    <label class="layui-form-label required">所属父级</label>
                    <div class="layui-input-block">
                        <select name="parentId" id="categoryId" lay-verify="categoryId" lay-filter="parentId">
                            <option value="0">请选择父级菜单</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label required">栏目标题</label>
                    <div class="layui-input-block">
                        <input type="text" name="name" lay-verify="required" value="@Model.Name" lay-reqtext="栏目名称不能为空" placeholder="请输入栏目名称" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label required">图标</label>
                    <div class="layui-upload">
                        <button type="button" class="layui-btn" id="test1">上传图片</button>
                        <div class="layui-upload-list">
                          @if (!string.IsNullOrEmpty(Model.IconSrc))
                                {
                                    <img class="layui-upload-img" id="demo1" src="@Model.IconSrc">
                                    <input type="hidden" id="iconSrc" name="iconSrc" class="layui-input" value="@Model.IconSrc">
                                }
                                else
                                {
                                    <img class="layui-upload-img" id="demo1">
                                    <input type="hidden" id="iconSrc" name="iconSrc" class="layui-input">
                                }  
                        </div>                                           
                    </div>
                </div>

                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</form>
@section js{
<script>
    layui.use(['form',  'common','upload'], function () {
        var form = layui.form,
            $ = layui.$,
            upload = layui.upload,
            apiUtil = layui.common;
            var id=@Model.Id;
            if (id > 0)
            {
                apiUtil.BindParentCategory(@Model.ParentId);
                 form.render();
            } else {
                apiUtil.BindParentCategory();
                 form.render();
            }
           
        var obj=function(idDom,imgDom,inputDom,fileDirectory){
               var model= {
                    elem: "#"+idDom
                    , accept: 'file' //普通文件
                    , url: apiUtil.apiUrl() + 'sys/upload/file'
                    , before: function (obj) {
                        //预读本地文件示例，不支持ie8
                        obj.preview(function (index, file, result) {
                            $("#"+imgDom).attr('src', result);
                        });
                    }
                     ,method:"post"
                     ,data:{
                         directory:function () {return fileDirectory;},
                     }
                    , done: function (res) {
                        if (res.success == true) {
                            console.log(res.data);
                            $("#"+imgDom).attr('src', res.data);
                            $("#"+inputDom).val(res.data);
                        }else{
                        layer.msg(res.msg);
                        return false;
                        }
                        //上传成功
                    }, error: function () {
                           layer.msg("上传失败了，请稍后重试！");
                        }
                }
                return model;
            };

            upload.render(
            obj("test1","demo1","iconSrc","category")
            );
                 var parentIndex =  parent.layer.getFrameIndex(window.name); 
        //监听提交
        form.on('submit(saveBtn)', function (data) {
            if (data.field.id>0) {
                apiUtil.ajax('shop/Category/modify', data.field, "application/json", "put", function (res) {
                    if (res.statusCode == 200 && res.success == true) {
                        apiUtil.success(res.msg);
                        parent.layer.close(parentIndex);
                    } else {
                        apiUtil.error(res.msg);
                    }
                });
            } else {
                apiUtil.ajax('shop/Category/add', data.field, "application/json", "post", function (res) {
                    if (res.statusCode == 200 && res.success == true) {
                        apiUtil.success(res.msg);
                        parent.layer.close(parentIndex);
                    } else {
                        apiUtil.error(res.msg);
                    }
                });
            }
            return false;
        });
    });

</script>

}


